#!/usr/bin/python
# -*- coding: utf-8 -*-
import os,sys
import stat

#=========================================================
def getFiles(rep,exts=[]):
#=========================================================
    assert type(rep)==unicode
    files = []
    dirs=[]
    if os.path.isdir(rep):
        for i in os.listdir(rep):
            if i[0]!=".":
                file = os.path.join(rep,i)
                if os.path.isfile(file):
                    n,ext = os.path.splitext(i)
                    if exts:
                        if ext:
                            if ext[1:].lower() in exts:
                                files.append(file)
                    else:
                        files.append(file)
                else:
                    dirs.append(file)

    dirs.sort(lambda a,b: cmp(a.lower(),b.lower()))
    files.sort(lambda a,b: cmp(a.lower(),b.lower()))
    
    return dirs,files   # UNICODE



def walktree (top = ".", depthfirst = True):
    """This walks a directory tree, starting from 'top'.
    This is somewhat like os.path.walk, but using
    generators instead of a visit function.
    One important difference is that walktree() defaults 
    to DEPTH first with optional BREADTH first,
    whereas the os.path.walk function allows only BREADTH first.
    Depth first was made the default because it is safer if
    you are going to be modifying directory names. This avoids
    the problem of renaming a directory before visiting the children
    of that directory.

    """
    names = os.listdir(top)
    if not depthfirst:
        yield top, names
    for name in names:
        try:
            st = os.lstat(os.path.join(top, name))
        except os.error:
            continue
        if stat.S_ISDIR(st.st_mode):
            for (newtop, children) in walktree (os.path.join(top, name), depthfirst):
                yield newtop, children
    if depthfirst:
        yield top, names

#=========================================================
def getAllFiles(rep,exts=[]):
#=========================================================
    assert type(rep)==unicode
    files = []
    for (basepath, children) in walktree(rep,False):
        for child in children:
            if child[0]!=".":
                file = os.path.join(basepath,child)
                n,ext = os.path.splitext(child)
                if exts:
                    if ext:
                        if ext[1:].lower() in exts:
                            files.append(file)
                else:
                    files.append(file)
    return files    # UNICODE
            



#=========================================================
def getExtVlcOpt(filename):
#=========================================================
    assert type(filename)==unicode
    opt_jpg = [
    "sout=#transcode:std",
    "sout-transcode-width=720",
    "sout-transcode-height=576",
    "sout-transcode-vb=9000",
    "sout-transcode-vcodec=mp2v",
    "sout-transcode-vt=1000000",
    "sout-transcode-vfilter=deinterlace",
    "sout-deinterlace-mode=blend",
    "sout-ffmpeg-keyint=1"
    ]
    
    opt_dvd = [
    "sout=#transcode:std",
    "sout-transcode-ab=384",
    "sout-transcode-acodec=mpga",
    "sout-transcode-channels=2",
    "file-caching=1000"
    ]
    
    opt_avi = [
    "sout=#transcode:std",
    "sout-transcode-ab=384",
    "sout-transcode-acodec=mpga",
    "sout-transcode-channels=2",
    "sout-transcode-vb=9000",
    "sout-transcode-vcodec=mp2v",
    "sout-transcode-vt=1000000",
    "file-caching=1000"
    ]

    #print "#EXTM3U\n";
  
    #~ filename = os.path.abspath(filename)
    ext= filename.split(".")[-1].lower()
    
    
    com=["#EXTINF:0,%s" % os.path.basename(filename)]
    
    com.append("#file:%s"%filename)     # self-add ;-)
    
    if ext in [".vob"]:
        com += ["#EXTVLCOPT:"+i for i in opt_dvd]
        com.append(filename)
    elif ext in "avi|ogg|mp3|mkv|mp4|mov|aac|au|aif|aiff|mjpeg|wmv|wma|wav|asf".split("|"):
        com += ["#EXTVLCOPT:"+i for i in opt_avi]
        com.append(filename)
    elif ext in "jpg|jpeg|png|tiff|gif".split("|"):
        com += ["#EXTVLCOPT:"+i for i in opt_jpg]
        com.append( "#EXTVLCOPT:fake-file=%s" % filename)
        com.append( "fake:")
    elif filename[:5] =="/dev/":
        com += ["#EXTVLCOPT:"+i for i in opt_dvd]
        com.append( "dvdsimple:"+filename)
    elif filename[:7]=="http://":
        com += ["#EXTVLCOPT:"+i for i in opt_avi]
        com.append(filename)
    else:
        com.append(filename)
    return com


if __name__ == "__main__":
    print getFiles(u"/media/data/photos/2007/02/verbier_2007 A TAGGUER",["jpg"])[1]
    #print getAllFiles(u"/media/d/divx",["avi"])
    
